package flex.cc.vehicle_svr.controller;

import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import flex.cc.PublicUtil;
import flex.cc.vehicle_svr.service.CancelService;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import flex.cc.common.utils.DateUtil;
import flex.cc.core.support.BaseController;
import flex.cc.order.manage.dto.vo.OrderMainVo;
import flex.cc.util.EncryptionDecryption;

/**
 * 上游取消接口
 */
@RestController
@RequestMapping(value = "/vehicleSvr")
public class CancelController extends BaseController {


    @Autowired
    private CancelService cancelService;

    /**
     * 上游取消接口
     */
    @SuppressWarnings("static-access")
	@ResponseBody
    @RequestMapping("/cancelOrder")
    public String cancelOrder(HttpServletRequest request) throws Exception {
    	// 进入接口时间
    	long startTime = System.currentTimeMillis();
    	logger.info("vehicleSvr/cancelOrder 进入接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));

    	Boolean flag = true;
        String responseMsg = "成功";
        EncryptionDecryption des = new EncryptionDecryption();
        //读取报文
        try{
        	// 设置编码
            request.setCharacterEncoding("UTF-8");
            // 读取请求报文
	        String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
	        requestJson = URLDecoder.decode(requestJson, "UTF-8");
	        if(requestJson != null && !"".equals(requestJson)) {
	        	// 调用业务处理 =============================
	        	logger.info("vehicleSvr/cancelOrder requestJson:" + requestJson);
	        	requestJson = des.decrypt(requestJson, "UTF-8");
            	logger.info("取消报文解密后=" + requestJson);
            	JSONObject jsonObject = JSONObject.parseObject(requestJson);
                JSONObject requestHeader = (JSONObject)jsonObject.get("requestHeader");
                JSONObject requestBody = (JSONObject)jsonObject.get("requestBody");
                Map requestHeaderMap = requestHeader.toJavaObject(Map.class);
                String requestCode = PublicUtil.isNotEmpty(requestHeaderMap.get("requestCode")) ? requestHeaderMap.get("requestCode").toString() : null;

                if ("cancelRescue".equals(requestCode)) {
                    Map requestBodyMap = requestBody.toJavaObject(Map.class);
                    String result = cancelService.cancelOrder(requestBodyMap);
                    if (result != null) {
                        JSONObject resultObject = JSONObject.parseObject(result);

                        System.out.println("resultObject: " + result);
                        Integer resultCode = Integer.valueOf(PublicUtil.isNotEmpty(resultObject.get("resultCode")) ? resultObject.get("resultCode").toString() : null);
                        String message = PublicUtil.isNotEmpty(resultObject.get("message")) ? resultObject.get("message").toString() : null;
                        if (resultCode == 0) {
                            flag = true;
                            responseMsg = "取消成功";
                        } else {
                            flag = false;
                            responseMsg = message;
                        }
                    } else {
                        flag = false;
                        responseMsg = "取消接口请求异常";
                    }

                } else {
                    flag = false;
                    responseMsg = "取消原因为空";
                }
	        } else {
                flag = false;
                responseMsg = "接口处理异常:请求报文为空";
	        	logger.error("vehicleSvr/cancelOrder 接口处理异常:请求报文为空");
	        }
        }catch(Exception e){
        	e.printStackTrace();
            flag = false;
            responseMsg = "数据接收异常";
        	logger.error("vehicleSvr/cancelOrder 数据接收异常:" + e.getMessage());
        }
        // 生成响应报文
        String responseJson = "";
        Map<String, String> response = new HashMap<String, String>();
        if(flag){
            response.put("responseCode", "00");
        } else {
            response.put("responseCode", "01");
        }
        response.put("responseMsg", responseMsg);
        responseJson = JSON.toJSONString(response);
        logger.info("vehicleSvr/cancelOrder responseJson:" + responseJson);

        // 离开接口时间
        long endTime = System.currentTimeMillis();
        logger.info("vehicleSvr/cancelOrder 离开接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ";处理时长:" + (endTime - startTime));

        return des.encrypt(responseJson);
    }
    
    
  
}
